/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package application;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.fxml.Initializable;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class RepetitiveController implements Initializable {

	/**
	 * Initializes the controller class.
	 */

	@FXML
	private Label sitError;

	@FXML
	private TextField diveTitle;

	@FXML
	private Label depthError;

	@FXML
	private Label plan231;

	@FXML
	private Label depthLabel;

	@FXML
	private Button doneBtn;

	@FXML
	private Button addBtn;

	@FXML
	private TextField timeBox;

	@FXML
	private Label plan12;

	@FXML
	private Label plan1;

	@FXML
	private Label plan23;

	@FXML
	private ComboBox<Integer> depthBox;

	@FXML
	private Label plan2;

	@FXML
	private Label plan21;

	@FXML
	private Label plan11;

	@FXML
	private TextField sitBox;

	@FXML
	private Label plan22;

	@FXML
	private Label timeError;

	@FXML
	private Label profileError;

	@FXML
	private Label plan;

	private int userTime, userDepth, userSIT;
	private char next = '?';

	@FXML
	void createProfile(Event event) {
		if (Main.currUserProfile.length() > 0) {
			if (userSIT > 0 && userDepth > 0 && userTime > 0) {
				Main.planner.setUserSIT(userSIT);
				Main.planner.setUserDepthAndTime(userDepth, userTime);
				Main.currTable = Main.planner.addRepetitiveDive(Main.currTable);
				Main.userProfiles.put(Main.currUserProfile,
						Main.planner.getPlanner());

				Main.planner.endPlan();

				Main.gotoScene("Profile.fxml", doneBtn, this);
			}
		}
	}

	@FXML
	void getNextDive(Event event) {
		if (Main.currUserProfile.length() > 0) {
			if (userSIT > 0 && userDepth > 0 && userTime > 0) {
				Main.planner.setUserSIT(userSIT);
				Main.planner.setUserDepthAndTime(userDepth, userTime);
				Main.currTable = Main.planner.addRepetitiveDive(Main.currTable);

				Main.gotoScene("Repetitive.fxml", addBtn, this);
			}
		}
	}

	private void disableForm() {
		depthError.setText("");
		depthError.setVisible(false);
		timeError.setText("");
		timeError.setVisible(false);

		depthBox.setDisable(true);
		timeBox.setDisable(true);
		timeBox.setEditable(false);
	}

	private void enableForm() {
		depthError.setText("");
		depthError.setVisible(false);
		timeError.setText("");
		timeError.setVisible(false);

		depthBox.setDisable(false);
		timeBox.setDisable(false);
		timeBox.setEditable(true);
	}

	public void changeDepthBox(ObservableList<Integer> list) {
		depthBox.setItems(list);
	}

	public void setDepthBox() {
		Integer[] times = Main.planner.getSecondTable().get(
				Main.planner.getCurrLetterGroup());
		next = 'A';
		if (times != null) {
			for (int i = 0; i < times.length; i++) {
				if (times[i] <= userSIT) {
					next = (char) (next + i);
					break;
				}
			}

			switch (next) {
			case 'A':
				changeDepthBox(FXCollections.observableArrayList(40, 50, 60,
						70, 80, 90, 100, 110, 120, 130));
				break;
			case 'B':
				changeDepthBox(FXCollections.observableArrayList(40, 50, 60,
						70, 80, 90, 100, 110, 120));
				break;
			case 'C':
				changeDepthBox(FXCollections.observableArrayList(40, 50, 60,
						70, 80, 90, 100, 110));
				break;
			case 'D':
				changeDepthBox(FXCollections.observableArrayList(40, 50, 60,
						70, 80, 90, 100));
				break;
			case 'E':
				changeDepthBox(FXCollections.observableArrayList(40, 50, 60,
						70, 80, 90, 100));
				break;
			case 'F':
				changeDepthBox(FXCollections.observableArrayList(40, 50, 60,
						70, 80));
				break;
			case 'G':
				changeDepthBox(FXCollections
						.observableArrayList(40, 50, 60, 70));
				break;
			case 'H':
				changeDepthBox(FXCollections.observableArrayList(40, 50));
				break;
			case 'I':
				changeDepthBox(FXCollections.observableArrayList(40, 50));
				break;
			case 'J':
				changeDepthBox(FXCollections.observableArrayList(40));
				break;
			default:
				depthBox.setItems(FXCollections.emptyObservableList());
				break;
			}
		} else {
			next = '?';
			disableForm();
		}
	}

	@FXML
	void parseSIT(Event event) {
		int sit = 0;

		try {
			sit = Integer.parseInt(sitBox.getText().trim());

			if (sit < 10) {
				userSIT = 0;
				sitError.setText("Must be at least 10 min.");
				sitError.setVisible(true);
				this.disableForm();
			} else if (sit >= 10) {
				userSIT = sit;

				sitError.setVisible(false);
				this.setDepthBox();
				this.enableForm();
			}
		} catch (NumberFormatException nfe) {
			userSIT = 0;
			sitError.setText("Please enter a positive integer.");
			sitError.setVisible(true);
			this.disableForm();
		}
	}

	@FXML
	void validateTime(Event event) {
		if (next != '?' && userDepth > 0) {
			Integer[] pair = Main.planner.getThirdTable().get(next)
					.get(userDepth);

			int newTime = 0;

			try {
				newTime = Integer.parseInt(timeBox.getText().trim());

				if (newTime > 0 && newTime <= pair[1]) {
					userTime = newTime;

					timeError.setVisible(false);
				} else {
					timeError.setText("Time must be <= " + pair[1]);
					timeError.setVisible(true);
				}
			} catch (NumberFormatException nfe) {
				userTime = 0;

				timeError.setText("Enter a time...");
				timeError.setVisible(true);
			}
		}
	}

	@FXML
	void validateName(Event event) {
		String newProfile = diveTitle.getText().trim();

		if (!Main.userProfiles.keySet().contains(newProfile)
				&& newProfile.length() > 0) {
			Main.currUserProfile = newProfile;

			profileError.setVisible(false);
		} else {
			Main.currUserProfile = "";

			profileError.setVisible(true);
		}
	}

	@FXML
	void setDepth(Event event) {
		if (depthBox.getValue() == null) {
			userDepth = 0;
			depthError.setVisible(true);
		} else {
			userDepth = depthBox.getValue();
			depthError.setVisible(false);
		}
		validateTime(event);
		System.out.println("SUCCESS: Changed user depth input to " + userDepth);
	}

	@Override
	public void initialize(URL url, ResourceBundle rb) {
		diveTitle.setText(Main.currUserProfile);
		userTime = userDepth = userSIT = 0;
		this.disableForm();
	}

}
